package casa.depesos.app.com.http.req;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.List;

import casa.depesos.app.com.http.HttpReq;
import casa.depesos.app.com.http.Req;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.data.Relation;
import casa.depesos.app.com.http.parser.GetCodeParser;
import casa.depesos.app.com.utils.Constant;
import okhttp3.RequestBody;

public class SaveTouchReq implements HttpReq<GetCodeBean> {

    List<Relation> list;
    int option;

    public SaveTouchReq(List<Relation> list, int option) {
        this.list = list;
        this.option = option;
    }

    @Override
    public void req(ReqCallback callback) {//    /app/cust/saveRelatives
        Req.post("/app/professionalData/preserveKinshipInfo", getParam(), getHeader(),
                getReqBody(), new GetCodeParser(), callback, this);
    }

    private RequestBody getReqBody() {
        JsonObject e = JsonParser.parseString("{}").getAsJsonObject();
        e.addProperty("pnum", "");
        e.addProperty("reqnum", Constant.getApplyId());

        e.addProperty("decision", option);

        JsonArray a = JsonParser.parseString("[]").getAsJsonArray();
        JsonObject obj = JsonParser.parseString("{}").getAsJsonObject();
        obj.addProperty("contype", list.get(0).relation);
        obj.addProperty("primaryConnection", 1);
        obj.addProperty("pname", list.get(0).name);
        obj.addProperty("mbileT", list.get(0).phone);
        obj.addProperty("idcode", "");
        a.add(obj);

        obj = JsonParser.parseString("{}").getAsJsonObject();
        obj.addProperty("contype", list.get(1).relation);
        obj.addProperty("primaryConnection", 1);
        obj.addProperty("pname", list.get(1).name);
        obj.addProperty("mbileT", list.get(1).phone);
        obj.addProperty("idcode", "");
        a.add(obj);

        e.add("relReqs", a);

        a = JsonParser.parseString("[]").getAsJsonArray();
        obj = JsonParser.parseString("{}").getAsJsonObject();
        obj.addProperty("relationShip", list.get(0).relation);
        obj.addProperty("mainShip", 1);
        obj.addProperty("contactName", list.get(0).name);
        obj.addProperty("mobileTel", list.get(0).phone);
        obj.addProperty("id", "");
        a.add(obj);

        obj = JsonParser.parseString("{}").getAsJsonObject();
        obj.addProperty("relationShip", list.get(1).relation);
        obj.addProperty("mainShip", 1);
        obj.addProperty("contactName", list.get(1).name);
        obj.addProperty("mobileTel", list.get(1).phone);
        obj.addProperty("id", "");
        a.add(obj);
        e.addProperty("connReq", a.toString());

        return RequestBody.create(Req.JSON, e.toString());
    }
}
